package client

import (
	"context"
	"fmt"
	"gitee.com/enjoy_5/sigin-rpc/constant"
	pb "gitee.com/enjoy_5/sigin-rpc/proto/invite"
	"gitee.com/xuxinzhi2206/corehub/nacos"
	"google.golang.org/grpc"
)

func ClientInviter(hand func(c pb.InviterClient) error) error {
	service, err := nacos.FindHealthService(constant.SERVICE2, constant.GroupName2)
	if err != nil {
		return fmt.Errorf("find server err:%v", err)
	}
	conn, err := grpc.Dial(service, grpc.WithInsecure())
	if err != nil {
		return fmt.Errorf("connect server err:%v", err)
	}
	defer conn.Close()
	c := pb.NewInviterClient(conn)
	return hand(c)
}
func CreateInvite(in *pb.InviteCreateRequest) (resp *pb.InviteCreateResponse, err error) {
	return resp, ClientInviter(func(ClientInviter pb.InviterClient) error {
		resp, err = ClientInviter.InviteCreate(context.Background(), in)
		if err != nil {
			return err
		}
		return nil
	})
}
func GetInviteInfo(in *pb.GetInviteRequest) (resp *pb.GetInviteResponse, err error) {
	return resp, ClientInviter(func(client pb.InviterClient) error {
		resp, err = client.GetInvite(context.Background(), in)
		if err != nil {
			return err
		}
		return nil
	})
}
